import { Controller, Get, Post, Body } from '@nestjs/common';
import * as udpService from './udp.service';

@Controller('udp')
export class UdpController {
  constructor(private readonly udpService: udpService.UdpService) {}

  // 获取所有连接的客户端
  @Get('clients')
  getClients() {
    return this.udpService.getConnectedClients();
  }

  // 发送消息到指定客户端
  @Post('send')
  async sendMessage(
    @Body() data: { address: string; port: number; message: string },
  ) {
    const bytes = await this.udpService.sendMessageToClient(
      data.address,
      data.port,
      data.message,
    );
    return { success: true, bytesSent: bytes };
  }

  // 广播消息到所有客户端
  @Post('broadcast')
  async broadcastMessage(@Body('message') message: udpService.UdpMessage) {
    await this.udpService.broadcastJsonMessage(message);
    return { success: true, message: '消息已广播' };
  }
}
